package wiz.client;

import wiz.client.model.WizGameModel;
import wiz.client.util.UUID;

import com.google.gwt.storage.client.Storage;

public class LocalStorageWizGameStorage implements WizGameStateStorage {

	private ClientFactory fClientFactory;
	private Storage fStorage;
	
	public LocalStorageWizGameStorage(Storage storage, ClientFactory clientFactory) {
		fClientFactory = clientFactory;
		fStorage = storage;
	}

	@Override
	public String storeGameState(WizGameModel model) {
		String newID = UUID.uuid();
		String modelJSON = fClientFactory.getWizModelFactory().modelToJSON(model);
		fStorage.setItem(newID, modelJSON);
		return newID;
	}

	@Override
	public WizGameModel loadGameState(String stateToken) {
		String state = fStorage.getItem(stateToken);
		if(state == null){
			return null;
		} else {
			return fClientFactory.getWizModelFactory().jsonToModel(state);
		}
	}
}
